package com.ruoyi.system.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.system.domain.AcIndex;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.AcWorkOrder;
import com.ruoyi.system.service.IAcWorkOrderService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import redis.clients.jedis.Jedis;

/**
 * 保养维护工单Controller
 *
 * @author ruoyi
 * @date 2025-03-04
 */
@RestController
@RequestMapping("/system/order")
@Anonymous
public class AcWorkOrderController extends BaseController
{
    @Autowired
    private IAcWorkOrderService acWorkOrderService;

    /**
     * 查询保养维护工单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:list')")
    @ApiOperation("查询保养维护工单列表")
    @GetMapping("/list")
    public TableDataInfo list(AcWorkOrder acWorkOrder)
    {
        startPage();
        List<AcWorkOrder> list = acWorkOrderService.selectAcWorkOrderList(acWorkOrder);
        return getDataTable(list);
    }

    /**
     * 导出保养维护工单列表
     */
    @PreAuthorize("@ss.hasPermi('system:order:export')")
    @Log(title = "保养维护工单", businessType = BusinessType.EXPORT)
    @ApiOperation("导出保养维护工单列表")
    @PostMapping("/export")
    public void export(HttpServletResponse response, AcWorkOrder acWorkOrder)
    {
        List<AcWorkOrder> list = acWorkOrderService.selectAcWorkOrderList(acWorkOrder);
        ExcelUtil<AcWorkOrder> util = new ExcelUtil<AcWorkOrder>(AcWorkOrder.class);
        util.exportExcel(response, list, "保养维护工单数据");
    }

    /**
     * 获取保养维护工单详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("获取保养维护工单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(acWorkOrderService.selectAcWorkOrderById(id));
    }

    /**
     * 新增保养维护工单
     */
    @PreAuthorize("@ss.hasPermi('system:order:add')")
    @Log(title = "保养维护工单", businessType = BusinessType.INSERT)
    @ApiOperation("新增保养维护工单")
    @PostMapping
    public AjaxResult add(@RequestBody AcWorkOrder acWorkOrder)
    {
        return toAjax(acWorkOrderService.insertAcWorkOrder(acWorkOrder));
    }

    /**
     * 修改保养维护工单
     */
    @PreAuthorize("@ss.hasPermi('system:order:edit')")
    @Log(title = "保养维护工单", businessType = BusinessType.UPDATE)
    @ApiOperation("修改保养维护工单")
    @PutMapping
    public AjaxResult edit(@RequestBody AcWorkOrder acWorkOrder)
    {
        return toAjax(acWorkOrderService.updateAcWorkOrder(acWorkOrder));
    }

//    查看redis列表
    @GetMapping("/redis")
    public AjaxResult redis(){
        Jedis jedis = new Jedis("127.0.0.1", 6379);
        jedis.select(6);
        List assetWorkOrderList = new ArrayList();


        try {
            if (jedis.exists("assetWorkOrder")) {
                // 如果 assetWorkOrder 列表已经存在，查看长度
                int length = jedis.llen("assetWorkOrder").intValue();
// 从 Redis 中获取列表所有元素
                assetWorkOrderList = jedis.lrange("assetWorkOrder", 0, -1);
            }  else {
                return success(assetWorkOrderList);
            }
        } catch (Exception e) {
            // 处理 Redis 操作异常
            System.err.println("Failed to add data to Redis: " + e.getMessage());
        } finally {
            // 关闭连接
            if (jedis != null) {
                jedis.close();
            }
        }

        return success(assetWorkOrderList);
}

    /**
     * 删除保养维护工单
     */
    @PreAuthorize("@ss.hasPermi('system:order:remove')")
    @Log(title = "保养维护工单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除保养维护工单")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(acWorkOrderService.deleteAcWorkOrderByIds(ids));
    }
    /*
    * 首页前四个数据项目
    * */
    /**
     * 获取首页四个数据项
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("获取首页四个数据项")
    @GetMapping(value = "/getIndexInfo")
    public AjaxResult getIndexInfo(AcIndex acIndex)
    {
        return success(acWorkOrderService.getIndexInfo( acIndex));
    }

    /*
     * 房产状态
     * */
    /**
     * 房产状态
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("房产状态")
    @GetMapping(value = "/getAssetStatus")
    public AjaxResult getAssetStatus(AcIndex acIndex)
    {
        return success(acWorkOrderService.getAssetStatus(acIndex));
    }

    /**
     * 巡检工单
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("巡检工单")
    @GetMapping(value = "/getWorkOrderStatus")
    public AjaxResult getWorkOrderStatus(AcIndex acIndex)
    {
        return success(acWorkOrderService.getWorkOrderStatus(acIndex));
    }

    /**
     * 合约签约分析
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("合约签约分析")
    @GetMapping(value = "/getContractStatus")
    public AjaxResult getContractStatus(AcIndex acIndex)
    {
        return success(acWorkOrderService.getContractStatus(acIndex));
    }

    /**
     * 合约到期预警
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("合约到期预警")
    @GetMapping(value = "/getContractEnd")
    public AjaxResult getContractEnd(AcIndex acIndex)
    {
        return success(acWorkOrderService.getContractEnd(acIndex));
    }

    /**
     * 保养维修工单
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("保养维修工单")
    @GetMapping(value = "/getWorkOrder")
    public AjaxResult getWorkOrder(AcIndex acIndex)
    {
        return success(acWorkOrderService.getWorkOrder(acIndex));
    }


    /**
     * 合同金额分析
     */
//    @PreAuthorize("@ss.hasPermi('system:order:query')")
    @ApiOperation("合同金额分析")
    @GetMapping(value = "/getContractRent")
    public AjaxResult getContractRent(AcIndex acIndex)
    {
        return success(acWorkOrderService.getContractRent(acIndex));
    }
//    @ApiOperation("报警消息推送")
//    @GetMapping(value = "/warnTask")
//    public void warnTask(){
//        acWorkOrderService.warnTask();
//    }
}
